###############################################################################
# 
#  Copyright (c) 2005, 2006, 2007, 2008, 2009
#  University of Rochester
#  Department of Computer Science
#  All rights reserved.
# 
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
# 
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
# 
#     * Neither the name of the University of Rochester nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
# 
# 
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.

# Path to the RSTM root, change this if necessary.
RSTM = ..


# We need the Makefile flags that are generated by stmconfig.
include $(RSTM)/Makefile.inc



# Change these if you need to for any reason.
AR ?= ar
CP ?= cp
RM ?= rm



# Include the stmconfig generated config.h when building.
CXXFLAGS += -I. -I$(RSTM)/ -include config.h


# We copy the built library into this directory, renaming it along the
# way. Then we generate an object file that has the stubs that STAMP needs, and
# merge it into this library. This ensures that we have everything STAMP needs
# during linking.
LIBSTM = $(RSTM)/stm/obj/libstm.a


# Just one file.
SOURCE = rstm.cpp
OBJECT = rstm.o
TARGET = librstm.a


.PHONY    = all, ee, el, ll, copy, clean


all: $(TARGET)


# Build librstm.a normally. We copy in the libstm.a generated during the
# regular stm build, renaming it librstm.a and then we build rstm.o, which we
# then merge into the library. Finally we delete the now useless .o.
$(TARGET): $(SOURCE) $(LIBSTM)
	$(CXX) $(CXXFLAGS) -c $< -o $(OBJECT)
	$(CP) $(LIBSTM) $(TARGET)
	@$(AR) d $(TARGET) GCHeap.o # GCHeap has no symbols, suppress error
	$(AR) ru $@ $(OBJECT)
	@$(RM) $(OBJECT)


# Builds libstm with a custom write acquisition and allocation
# strategy. Otherwise the same as the normal build.
ee el ll: $(SOURCE) $(LIBSTM)
	$(CXX) $(CXXFLAGS) -DVERSIONING=$@ -c $< -o $(OBJECT)
	$(CP) $(LIBSTM) $(TARGET)
	@$(AR) d $(TARGET) GCHeap.o # GCHeap has no symbols, suppress error
	$(AR) ru $(TARGET) $(OBJECT)
	@$(RM) $(OBJECT)


clean:
	$(RM) -f $(TARGET) $(OBJECT)
